<--- %%NOBANNER%% --> wordclose.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Close a word document;                                             |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| Arguments:                                                         |
|    wordref: word reference; not necessary, default is "wordsys";   |
|-------------<-- End of Files or Arguments Needed-->----------------|
|------------------<-- Start of Files Created-->---------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: %wordclose; / %wordclose(wordsys);                        |
| Usage:   %wordclose(wordref);                                      |
\-------------------<-- End of Files Created-->---------------------*/
%macro wordclose/parmbuff;
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  6-4-2001 11:22pm;                 |
| Modified: 1-15-2002 10:50pm;                |
| Purpose:  Close the word document;          |
\--------------------------------------------*/
%local arg fref wsid rc;
%let arg=%qscan(&syspbuff,1,%str((),));
%let fref=; %let wsid=0;
%if (%index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(WORDREF=))) or 
    (%index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(WINREF=))) or 
    (%index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(WINDOW=))) or
    (%index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(WIN=)))
      %then %do;
   %let fref=%qscan(&arg, 2, %str(=));
   %put --> Note: Checking the status of window "&fref" to see if it is opened.;
%end;
%else %if (%quote(&arg) ne) %then %do;
   %if not (%index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(=))) %then %do; 
      %let fref=%sysfunc(dequote(&arg));
      %put --> Note: Checking the status of window "&fref" to see if it is open.;
   %end;
   %else %do;
      %put --> Alert! Keyword Parameter isn%str(%')t declared!;
   %end;
%end;
%else %if (%quote(&arg) eq) %then %do;
   Filename wordsys dde 'winword|system' lrecl=1048576;
   %let fref=wordsys;
   %put --> Note: No window reference is specified%str(;) Checking the status of the default; 
   %put -->       "&fref" to see if it is open.;
%end;
%if (%quote(&fref) ne) %then %do;
   %if (%index(%quote(&fref), %quote(\)) or %index(%quote(&fref), %quote(/))) %then %do;
      FILENAME wordsys dde "winword|&fref" command lrecl=1048576;
      %let fref=wordsys;
   %end;
   %let wsid=%sysfunc(fopen(&fref,s));
%end; 
%if &wsid %then %do;
   %let rc=%sysfunc(fclose(&wsid));
   data _tmpsys_;
      file &fref;
      put '[FileExist]';
   run; 
%end;
%else %do;
   %put ==> Alert! Incorrect window reference "&fref", or window "&fref" isn%str(%')t open.;
%end;
%mend wordclose;